<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="领料单号">
              <a-input v-model="queryParam.serial" placeholder="请输入领料单号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="红蓝单">
              <j-dict-select-tag
                v-model="queryParam.isRed"
                placeholder="请选择红蓝单"
                dict-code="hf_if_red"
              ></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="入库类型">
              <j-dict-select-tag
                v-model="queryParam['pickingType']"
                placeholder="请选择入库类型"
                dict-code="hf_picking_type"
              ></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="领料部门">
              <j-dict-select-tag
                v-model="queryParam['pickingDepartment']"
                placeholder="请选择领料部门"
                dict-code="sys_depart,depart_name,id"
              ></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="单据日期">
              <j-date
                date-format="YYYY-MM-DD"
                v-model="queryParam['_documentDate']"
                @change="handleDateInterval"
                placeholder="请选择单据日期"
                show-time
                style="width: 100%"
                :disabled="queryParam['_dateInterval']"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="日期区间">
              <a-range-picker
                v-model="queryParam['_dateInterval']"
                @change="handleDateInterval"
                :disabled="queryParam['_documentDate']"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="物料代码">
              <a-input v-model="queryParam.materialCode" placeholder="请输入物料代码" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="物料名称">
              <a-input v-model="queryParam.materialName" placeholder="请输入物料名称" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="关联的生产任务单号">
              <a-input v-model="queryParam['productOrderSerial']" placeholder="请输入关联的生产任务单号" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('领料单')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="isClosed" slot-scope="text">
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.status === 0">编辑</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleDelete(record.id)" v-if="record.status === 0">删除</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleUndoCheck(record)" v-if="record.status === 1">反审核</a>
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleCheck(record)" v-if="record.status === 0">审核</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleClose(record)" v-if="record.isClosed === 0">关闭</a>
          <a @click="handleUndoClose(record)" v-if="record.isClosed === 1">反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="生产领料明细" key="1">
        <HfProductionPickingDetailList :mainId="selectedMainId" ref="detailList" />
      </a-tab-pane>
    </a-tabs>

    <hfProductionPicking-modal ref="modalForm" @ok="modalFormOk"></hfProductionPicking-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfProductionPickingModal from './modules/HfProductionPickingModal'
import { getAction } from '@/api/manage'
import HfProductionPickingDetailList from './HfProductionPickingDetailList'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import GenerateProductAllocate from '@views/production/modules/GenerateProductAllocate.vue'
import store from '@/store'

export default {
  name: 'HfProductionPickingList',
  mixins: [JeecgListMixin],
  components: {
    GenerateProductAllocate,
    HfProductionPickingDetailList,
    HfProductionPickingModal
  },
  data() {
    return {
      description: '生产领料管理页面',
      // 表头
      columns: [
        {
          title: '领料单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate'
        },
        // {
        //   title: '领料单位',
        //   align: 'center',
        //   dataIndex: 'pickingDepartment_dictText'
        // },
        {
          title: '红蓝单',
          align: 'center',
          dataIndex: 'isRed_dictText'
        },
        {
          title: '领料类型',
          align: 'center',
          dataIndex: 'pickingType_dictText'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed_dictText',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '所属生产指令单号',
          align: 'center',
          dataIndex: 'productMissionSerial'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/production/hfProductionPicking/list',
        delete: '/production/hfProductionPicking/delete',
        deleteBatch: '/production/hfProductionPicking/deleteBatch',
        exportXlsUrl: '/production/hfProductionPicking/exportXls',
        importExcelUrl: 'production/hfProductionPicking/importExcel',
        check: '/production/hfProductionPicking/check',
        undoCheck: '/production/hfProductionPicking/undoCheck',
        close: '/production/hfProductionPicking/close',
        undoClose: '/production/hfProductionPicking/undoClose'
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: []
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {},
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.$refs.detailList.isChecked = true
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let params = this.getQueryParams() //查询条件
      params['status'] = 1
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            this.ipagination.total = res.result.total
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleDateInterval(value) {
      if (typeof value === 'string') {
        this.queryParam['documentDate_begin'] = value
        this.queryParam['documentDate_end'] = value
        return
      }
      this.queryParam['documentDate_begin'] = value[0].format('YYYY-MM-DD')
      this.queryParam['documentDate_end'] = value[1].format('YYYY-MM-DD')
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
